namespace C1.Util.Licensing
{
    using System;
    using System.ComponentModel;
    using System.IO;
    using System.Reflection;
    using System.Security.Cryptography;
    using System.Text;
    using System.Windows.Forms;

    public class ProviderInfo
    {
        private Component _component;
        private static byte[] _iv;
        private string _licToken;
        private static bool _nagged;
        private Type _type;

        public ProviderInfo(Type type, Component instance)
        {
            this._type = type;
            this._component = instance;
        }

        private static string Decrypt(string cypher)
        {
            string str;
            try
            {
                byte[] buffer = Convert.FromBase64String(cypher);
                using (Rijndael rijndael = Rijndael.Create())
                {
                    using (ICryptoTransform transform = rijndael.CreateDecryptor(GetIV(), GetIV()))
                    {
                        using (MemoryStream stream = new MemoryStream(buffer))
                        {
                            using (CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Read))
                            {
                                using (StreamReader reader = new StreamReader(stream2))
                                {
                                    str = reader.ReadToEnd();
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
                str = null;
            }
            return str;
        }

        private static string Encrypt(string plainText)
        {
            string str;
            using (Rijndael rijndael = Rijndael.Create())
            {
                using (ICryptoTransform transform = rijndael.CreateEncryptor(GetIV(), GetIV()))
                {
                    using (MemoryStream stream = new MemoryStream())
                    {
                        using (CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write))
                        {
                            byte[] bytes = Encoding.UTF8.GetBytes(plainText);
                            stream2.Write(bytes, 0, bytes.Length);
                            stream2.FlushFinalBlock();
                            bytes = stream.ToArray();
                            str = Convert.ToBase64String(bytes, 0, bytes.Length);
                        }
                    }
                }
            }
            return str;
        }

        private static byte[] GetIV()
        {
            if (_iv == null)
            {
                _iv = Encoding.ASCII.GetBytes("ComponentOne LLC");
            }
            return _iv;
        }

        public string LicenseToken
        {
            get
            {
                return this._licToken;
            }
            set
            {
                this._licToken = value;
                string[] strArray = new string[0];
                if (!string.IsNullOrEmpty(value))
                {
                    strArray = Decrypt(value).Split(new char[] { '\t' });
                }
                if ((strArray.Length >= 4) && (strArray[0].Length == 2))
                {
                    foreach (C1ProductInfoAttribute attribute in this._type.Assembly.GetCustomAttributes(typeof(C1ProductInfoAttribute), true))
                    {
                        if (attribute.ProductCode == strArray[0])
                        {
                            return;
                        }
                    }
                }
                if (!_nagged && ((this._component.Site == null) || !this._component.Site.DesignMode))
                {
                    _nagged = true;
                    object[] customAttributes = this._type.Assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), true);
                    int index = 0;
                    while (index < customAttributes.Length)
                    {
                        AssemblyTitleAttribute attribute2 = (AssemblyTitleAttribute) customAttributes[index];
                        string text = "Evaluation version.\r\nThis dialog will not be shown if you rebuild your application using a licensed version of the product.";
                        MessageBox.Show(text, attribute2.Title);
                        return;
                    }
                }
            }
        }
    }
}

